﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Styx;

namespace ShutUp_AIML
{
    public static class NotifyIconHelper
    {
        public static NotifyIcon NotifyIcn { get; set; }

        public static void Initialize()
        {
            NotifyIcn = new NotifyIcon();
            cMs_Opened(null, null);
            NotifyIcn.Icon = ChatHelper.convertToIcon(System.Drawing.Image.FromFile(Application.StartupPath + "\\Plugins\\ShutUpAIML\\Forms\\BarryDurex"),
                System.Drawing.Color.Empty);
            NotifyIcn.Text = "[ShutUp] - (" + StyxWoW.Me.Name + ")";
            //NotifyIcn.ContextMenuStrip = cMs; // new UpdateForm().contextMenuStrip1;
            NotifyIcn.Visible = false;

            NotifyIcn.MouseDoubleClick += new MouseEventHandler(NotifyIcn_MouseDoubleClick);
            NotifyIcn.Disposed += new EventHandler(NotifyIcn_Disposed);
            NotifyIcn.MouseMove += new MouseEventHandler(cMs_Opened); 

            ShutUp_AIML.dlog("[ShutUp] NotifyIconHelper - Initialize");
        }

        private static void NotifyIcn_Disposed(object obj, EventArgs args)
        {
            NotifyIcn.Visible = false;
        }

        private static void NotifyIcn_MouseDoubleClick(object obj, MouseEventArgs args)
        {
            var _configForm = new SettingsForm();
            _configForm.Show();
        }

        #region ContextMenuStrip

        //private static ContextMenuStrip cMs = new UpdateForm().contextMenuStrip1;
        private static ToolStripSeparator ts_sep { get { return new ToolStripSeparator(); } }
        private static ToolStripLabel ts_lb_emty { get { return new ToolStripLabel() { Text = " " }; } }

        private static void cMs_Opened(object obj, EventArgs args)
        {
            NotifyIcn.ContextMenuStrip = new ContextMenuStrip();

            NotifyIcn.ContextMenuStrip.BackColor = Color.Gainsboro;
            NotifyIcn.ContextMenuStrip.ShowImageMargin = false;

            ToolStripLabel ts_lb_ShutUp = new ToolStripLabel()
            {
                Text = "  [ShutUp]",
                //Dock = DockStyle.Top,
                TextAlign = ContentAlignment.MiddleCenter,
                ForeColor = Color.CornflowerBlue,
                Font = new Font(new FontFamily("Comic Sans MS"), (float)(ts_lb_emty.Font.Size + 8), FontStyle.Bold),
                Image = Image.FromFile(Application.StartupPath +  "\\Plugins\\ShutUpAIML\\Forms\\BarryDurex"),
                ImageAlign = ContentAlignment.MiddleLeft
            };

            ToolStripLabel ts_lb_wsp = new ToolStripLabel()
            {
                Text = "whisper received/answer:",
                Font = new Font(ts_lb_emty.Font.FontFamily, (float)(ts_lb_emty.Font.Size - 1.2))
            };

            ToolStripLabel ts_lb_wsp_friend = new ToolStripLabel() { Text = string.Format("Friends/Guild:    {0} / {1}", ChatHelper.received_friend, ChatHelper.answer_friend) };

            ToolStripLabel ts_lb_wsp_unknown = new ToolStripLabel() { Text = string.Format("Unknown:           {0} / {1}", ChatHelper.received_unk, ChatHelper.answer_unk) };

            ToolStripMenuItem ts_itm_Open = new ToolStripMenuItem()
            {
                Text = "Open",
                Font = new Font(ts_lb_wsp.Font, FontStyle.Bold)
            };

            ToolStripMenuItem ts_itm_awsFriend = new ToolStripMenuItem()
            {
                Text = "answer at friend/guild",
                ForeColor = Color.Red
            };

            ToolStripMenuItem ts_itm_awsUnknown = new ToolStripMenuItem()
            {
                Text = "answer at unknown",
                ForeColor = Color.Green
            };

            ToolStripMenuItem ts_itm_awsMyName = new ToolStripMenuItem()
            {
                Text = "answer at myName",
                ForeColor = Color.Red
            };

            if (ShutUpSettings.Instance.answerWisper_Friends)
                ts_itm_awsFriend.ForeColor = Color.Green;
            else
                ts_itm_awsFriend.ForeColor = Color.Red;

            if (ShutUpSettings.Instance.answerWisper_unknown)
                ts_itm_awsUnknown.ForeColor = Color.Green;
            else
                ts_itm_awsUnknown.ForeColor = Color.Red;

            if (ShutUpSettings.Instance.myName_answer)
                ts_itm_awsMyName.ForeColor = Color.Green;
            else
                ts_itm_awsMyName.ForeColor = Color.Red;

            ts_itm_Open.Click += new EventHandler(Open_onClick);
            ts_itm_awsFriend.Click += new EventHandler(ts_itm_awsFriend_Click);
            ts_itm_awsUnknown.Click +=new EventHandler(ts_itm_awsUnknown_Click);
            ts_itm_awsMyName.Click += new EventHandler(ts_itm_awsMyName_Click);


            NotifyIcn.ContextMenuStrip.Items.Add(ts_lb_ShutUp);
            //NotifyIcn.ContextMenuStrip.Items.Add(ts_lb_emty);
            NotifyIcn.ContextMenuStrip.Items.Add(ts_sep);

            NotifyIcn.ContextMenuStrip.Items.Add(ts_lb_wsp);
            NotifyIcn.ContextMenuStrip.Items.Add(ts_lb_wsp_friend);
            NotifyIcn.ContextMenuStrip.Items.Add(ts_lb_wsp_unknown);
            NotifyIcn.ContextMenuStrip.Items.Add(ts_sep);

            NotifyIcn.ContextMenuStrip.Items.Add(ts_itm_awsFriend);
            NotifyIcn.ContextMenuStrip.Items.Add(ts_itm_awsUnknown);
            NotifyIcn.ContextMenuStrip.Items.Add(ts_itm_awsMyName);


            NotifyIcn.ContextMenuStrip.Items.Add(ts_sep);
            //NotifyIcn.ContextMenuStrip.Items.Add(ts_lb_emty);
            NotifyIcn.ContextMenuStrip.Items.Add(ts_itm_Open);

            //NotifyIcn.ContextMenuStrip = cMs;
        }

        private static void Open_onClick(object obj, EventArgs args)
        {
            NotifyIcn_MouseDoubleClick(null, null);
        }

        private static void ts_itm_awsFriend_Click(object obj, EventArgs args)
        {
            if (ShutUpSettings.Instance.answerWisper_Friends)
                ShutUpSettings.Instance.answerWisper_Friends = false;
            else
                ShutUpSettings.Instance.answerWisper_Friends = true;
        }

        private static void ts_itm_awsUnknown_Click(object obj, EventArgs args)
        {
            if (ShutUpSettings.Instance.answerWisper_unknown)
                ShutUpSettings.Instance.answerWisper_unknown = false;
            else
                ShutUpSettings.Instance.answerWisper_unknown = true;
        }

        private static void ts_itm_awsMyName_Click(object obj, EventArgs args)
        {
            if (ShutUpSettings.Instance.myName_answer)
                ShutUpSettings.Instance.myName_answer = false;
            else
                ShutUpSettings.Instance.myName_answer = true;
        }

        #endregion
    }
}
